<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import httpInstance from '../../utils/http';
import router from '@/router';
import dayjs from 'dayjs';

const equipmentInfo = ref({});
const userInfo = ref({});

onMounted(() => {
  getUserData();
  getEquipmentData();
});

// 获取用户信息
const getUserData = () => {
  const userData = sessionStorage.getItem('userInfo');
  if (userData) {
    userInfo.value = JSON.parse(userData);
  } else {
    ElMessage.warning('未找到用户信息');
  }
};

// 获取设备信息
const getEquipmentData = () => {
  const equipmentData = sessionStorage.getItem('equipment');
  if (equipmentData) {
    equipmentInfo.value = JSON.parse(equipmentData);
    console.log("设备信息：", equipmentInfo.value);
  } else {
    ElMessage.warning('未找到设备信息');
  }
};

// 返回上一页
const goBack = () => {
  router.back();
};

// 格式化时间
const formatTime = (timeStr) => {
  if (!timeStr) return '-';
  return dayjs(timeStr).format('YYYY-MM-DD HH:mm:ss');
};

// 图片是否为空判断
const isImageEmpty = (imageUrls) => {
  return !imageUrls || imageUrls.length === 0;
};
</script>

<template>
  <div class="detail-page">
    <div class="detail-container">
      <h2 class="page-title">设备详情</h2>

      <div class="detail-content">
        <!-- 基本信息 -->
        <div class="info-section">
          <div class="info-item">
            <span class="label">品牌：</span>
            <span class="value">{{ equipmentInfo.brand }}</span>
          </div>
          <div class="info-item">
            <span class="label">型号：</span>
            <span class="value">{{ equipmentInfo.model }}</span>
          </div>
          <div class="info-item">
            <span class="label">类型：</span>
            <span class="value">{{ equipmentInfo.equipmentType?.typeName }}</span>
          </div>
          <div class="info-item">
            <span class="label">价格：</span>
            <span class="value">¥{{ equipmentInfo.price }}</span>
          </div>
          <div class="info-item">
            <span class="label">年份：</span>
            <span class="value">{{ equipmentInfo.year }}</span>
          </div>
          <div class="info-item">
            <span class="label">轴数：</span>
            <span class="value">{{ equipmentInfo.chassisAxle }}</span>
          </div>
          <div class="info-item">
            <span class="label">排放标准：</span>
            <span class="value">{{ equipmentInfo.emissionStandard }}</span>
          </div>
          <div class="info-item">
            <span class="label">支腿类型：</span>
            <span class="value">{{ equipmentInfo.outriggerType }}</span>
          </div>
          <div class="info-item">
            <span class="label">垂直高度：</span>
            <span class="value">{{ equipmentInfo.verticalHeight }}cm</span>
          </div>
          <div class="info-item">
            <span class="label">小时数：</span>
            <span class="value">{{ equipmentInfo.hour }}小时</span>
          </div>
          <div class="info-item">
            <span class="label">位置：</span>
            <span class="value">{{ equipmentInfo.location }}</span>
          </div>
          <div class="info-item">
            <span class="label">联系人：</span>
            <span class="value">{{ equipmentInfo.contactName }}</span>
          </div>
          <div class="info-item">
            <span class="label">联系电话：</span>
            <span class="value">{{ equipmentInfo.contactPhone }}</span>
          </div>
          <div class="info-item">
            <span class="label">创建时间：</span>
            <span class="value">{{ formatTime(equipmentInfo.createTime) }}</span>
          </div>
          <div class="info-item">
            <span class="label">发布时间：</span>
            <span class="value">{{ formatTime(equipmentInfo.releaseTime) }}</span>
          </div>
          <div class="info-item">
            <span class="label">更新时间：</span>
            <span class="value">{{ formatTime(equipmentInfo.updateTime) }}</span>
          </div>
          <div class="info-item">
            <span class="label">描述：</span>
            <span class="value">{{ equipmentInfo.description }}</span>
          </div>
        </div>

        <!-- 图片展示区 -->
        <div class="image-section">
          <div class="image-header">主图</div>
          <div class="image-container">
            <img
              v-if="equipmentInfo.equipmentImages && equipmentInfo.equipmentImages[0]"
              :src="equipmentInfo.equipmentImages[0].imageUrl"
              alt="主图"
              class="head-image"
            />
            <div v-else class="empty-image"></div>
          </div>

          <div class="image-header">图片列表</div>
          <div class="image-gallery">
            <div
              v-for="(img, index) in equipmentInfo.equipmentImages"
              :key="index"
              class="image-thumbnail"
              style="width: 100px; height: 80px; overflow: hidden; border-radius: 4px;"
            >
              <img :src="img.imageUrl" alt="缩略图" style="width: 100%; height: 100%; object-fit: cover;" />
            </div>
          </div>
        </div>

        <!-- 状态信息 -->
        <div class="status-section">
          <div class="info-item">
            <span class="label">状态：</span>
            <span class="value">{{ equipmentInfo.status === 3 ? '已售' : '待售' }}</span>
          </div>
          <div class="info-item">
            <span class="label">是否检测：</span>
            <span class="value">{{ equipmentInfo.isDetected ? '是' : '否' }}</span>
          </div>
        </div>
      </div>

      <!-- 返回按钮 -->
      <div class="return-button">
        <el-button @click="goBack">返回</el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 1. 新增页面外层容器，实现整体居中 */
.detail-page {
  width: 100%;
  min-height: calc(100vh - 40px);
  display: flex;
  justify-content: center;
  padding: 20px 0;
  background-color: #f5f7fa; /* 增加页面背景色，突出中间内容 */
}

/* 2. 内容容器限制最大宽度，避免过宽 */
.detail-container {
  width: 100%;
  max-width: 1000px; /* 比原1200px稍窄，更适合居中视觉 */
  padding: 0 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.page-title {
  color: #333;
  font-size: 22px;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
  margin-bottom: 25px;
  font-weight: 600;
  text-align: center; /* 标题单独居中，增强视觉焦点 */
}

.detail-content {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

/* 信息区块通用样式（不变） */
.info-section, .image-section, .status-section {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 20px 25px;
  transition: box-shadow 0.3s ease;
}

.info-section:hover, .image-section:hover, .status-section:hover {
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
}

/* 信息项样式（不变） */
.info-item {
  display: flex;
  padding: 12px 0;
  border-bottom: 1px dashed #f0f0f0;
}

.info-item:last-child {
  border-bottom: none;
}

.label {
  flex: 0 0 120px;
  color: #666;
  font-weight: 500;
  padding-right: 15px;
  text-align: right;
}

.value {
  flex: 1;
  color: #333;
  line-height: 1.6;
}

/* 图片区域样式（不变） */
.image-section {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.image-group {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.image-header {
  color: #666;
  font-weight: 500;
  font-size: 15px;
  padding-bottom: 5px;
  border-bottom: 1px solid #f0f0f0;
}

.image-container {
  width: 100%;
  height: 300px;
  border-radius: 6px;
  overflow: hidden;
  background: #f9f9f9;
  display: flex;
  align-items: center;
  justify-content: center;
}

.head-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.empty-image {
  width: 100%;
  height: 100%;
  background: #f9f9f9 url('https://picsum.photos/800/400') center no-repeat;
  background-size: contain;
}

.image-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  padding: 10px 0;
  justify-content: center; /* 图片缩略图居中排列 */
}

/* 返回按钮样式（不变） */
.return-button {
  margin-top: 30px;
  text-align: center;
}

.return-button .el-button {
  padding: 8px 24px;
  font-size: 14px;
  background: #409eff;
  color: #fff;
  border: none;
  transition: all 0.3s ease;
}

.return-button .el-button:hover {
  background: #66b1ff;
  transform: translateY(-2px);
}

/* 响应式调整（不变） */
@media (max-width: 768px) {
  .detail-container {
    max-width: 100%;
    margin: 0;
    padding: 0 10px;
  }

  .info-item {
    flex-direction: column;
    padding: 10px 0;
  }

  .label {
    flex: none;
    text-align: left;
    padding-bottom: 5px;
    font-size: 14px;
  }

  .image-container {
    height: 200px;
  }

  .page-title {
    font-size: 18px;
    margin-bottom: 15px;
  }
}
</style>